Imports System.Data
Imports System.Data.Common

Partial Class _Default
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            txtDate.Text = DateTime.Today.ToString("MMMM d, yyyy")
            SetLabelText()

            drpType.DataSource = GetTransationTypes()
            drpType.DataTextField = "TransactionType"
            drpType.DataValueField = "TransactionTypeID"
            drpType.DataBind()

            drpFromTo.DataSource = GetPersonNames()
            drpFromTo.DataTextField = "PersonName"
            drpFromTo.DataValueField = "PersonID"
            drpFromTo.DataBind()

            btnSave.Attributes.Add("OnClick", "return ConfirmationMessage();")

            Dim vno As Integer = GetVoucherNumber()
            ViewState("VNo") = vno.ToString()
            txtVoucher.Text = vno.ToString()



        End If
    End Sub

    Protected Sub SetLabelText()
        lblCash.Text = "Cash Payment"
        lblBank.Text = "Bank Payment"
        lblFromTo.Text = "Paid To"
        lblWithdrawn.Visible = True
        txtBankWithdraw.Visible = True
        lblHeader.Text = "Create New Payment Voucher"
        txtRecieptDtls.Visible = False
        If Not Request.QueryString("mode") Is Nothing Then
            If Request.QueryString("mode") = "r" Then
                lblCash.Text = "Cash Reciept"
                lblBank.Text = "Bank Reciept"
                lblFromTo.Text = "Deposited By"
                lblWithdrawn.Visible = False
                lblHeader.Text = "Create New Cash Reciept"
                txtBankWithdraw.Visible = False
                txtRecieptDtls.Visible = False ' will use later if required
            End If
        End If
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim isReciept As Boolean = False
        If Not Request.QueryString("mode") Is Nothing Then
            If Request.QueryString("mode") = "r" Then
                isReciept = True
            End If
        End If

        If (txtDesc.Text = "") Then
            lblMessage.Text = "Please provide description"
            Exit Sub
        End If

        If (txtCash.Text = "" And txtBank.Text = "") Then
            lblMessage.Text = "Please provide amount"
            Exit Sub
        End If

        Dim vchNo As Integer = Int32.Parse(txtVoucher.Text)

        Dim str As New StringBuilder
        str.Append(" insert into Voucher values( ")
        str.Append(vchNo.ToString() + " , '")
        str.Append(hdnDate.Value + "' ,  '")
        str.Append(txtDesc.Text + "' , ")
        str.Append(drpType.SelectedValue.ToString() + " , ")

        If isReciept Then
            str.Append(IIf(txtCash.Text = "", 0, txtCash.Text).ToString() + " , ")
            str.Append(IIf(txtBank.Text = "", 0, txtBank.Text).ToString() + " , ")
            str.Append(" 0, ")
            str.Append(" 0, ")
            str.Append(" 0 , ")
        Else
            str.Append(" 0, ")
            str.Append(" 0, ")
            str.Append(IIf(txtCash.Text = "", 0, txtCash.Text).ToString() + " , ")
            str.Append(IIf(txtBank.Text = "", 0, txtBank.Text).ToString() + " , ")
            str.Append(IIf(txtBankWithdraw.Text = "", 0, txtBankWithdraw.Text).ToString() + " , ")
        End If

        str.Append(drpFromTo.SelectedValue.ToString() + " , ")
        str.Append(" 0 , ")
        str.Append(" 0  ")
        str.Append(" ) ")

        DBHelper.ExecuteNonQuery(CommandType.Text, str.ToString())

        If Int32.Parse(ViewState("VNo").ToString()) = Int32.Parse(txtVoucher.Text) Then
            UpdateVoucherNumber(vchNo, isReciept)
        End If

        ' check if cash reciet was cash deposit sales
        If isReciept Then
            If drpType.SelectedValue.ToString() = "1" Then
                CreateVoidVoucherEntry(IIf(txtBank.Text = "", 0, txtBank.Text).ToString())
            End If
        End If

        If (hdnPrintVoucher.Value = "y") Then
            If isReciept Then
                Response.Redirect("reports.aspx?vno=" + vchNo.ToString() + "&rpt=r")

            Else
                Response.Redirect("reports.aspx?vno=" + vchNo.ToString() + "&rpt=p")
            End If

        End If

        ClearAllFields()

        lblMessage.Text = "Data Saved Successfully"

        Dim vno As Integer = GetVoucherNumber()
        ViewState("VNo") = vno.ToString()
        txtVoucher.Text = vno.ToString()


    End Sub

    Protected Function GetVoucherNumber()

        Dim isReciept As Boolean = False
        If Not Request.QueryString("mode") Is Nothing Then
            If Request.QueryString("mode") = "r" Then
                isReciept = True
            End If
        End If

        Dim val As Object
        If isReciept Then
            val = DBHelper.ExecuteScalar(CommandType.Text, "  SELECT RecieveNo FROM VoucherNumbers ")
        Else
            val = DBHelper.ExecuteScalar(CommandType.Text, "  SELECT PaymentNo FROM VoucherNumbers ")
        End If

        If IsDBNull(val) Then val = 0
        Dim vchNo As Integer = Int32.Parse(val.ToString())
        vchNo = vchNo + 1

        'If isReciept Then
        '    DBHelper.ExecuteNonQuery(CommandType.Text, "Update VoucherNumbers set RecieveNo= " + vchNo.ToString())
        'Else
        '    DBHelper.ExecuteNonQuery(CommandType.Text, "Update VoucherNumbers set PaymentNo= " + vchNo.ToString())
        'End If

        Return vchNo
    End Function

    Protected Sub UpdateVoucherNumber(ByVal vchNo As Integer, ByVal isReciept As Boolean)
        If isReciept Then
            DBHelper.ExecuteNonQuery(CommandType.Text, "Update VoucherNumbers set RecieveNo= " + vchNo.ToString())
        Else
            DBHelper.ExecuteNonQuery(CommandType.Text, "Update VoucherNumbers set PaymentNo= " + vchNo.ToString())
        End If
    End Sub

    Protected Sub CreateVoidVoucherEntry(ByVal amount As String)
        Dim str As New StringBuilder
        str.Append(" insert into Voucher values( ")
        str.Append(" 0 , '")
        str.Append(hdnDate.Value + "' ,  '")
        str.Append(txtDesc.Text + "' , ")
        str.Append(drpType.SelectedValue.ToString() + " , ")
        str.Append(" 0, ")
        str.Append(" 0, ")
        str.Append(amount + " , ")
        str.Append(" 0 , ")
        str.Append(" 0 , ")

        str.Append(" 8 , ")
        str.Append(" 0 , ")
        str.Append(" 0  ")
        str.Append(" ) ")

        DBHelper.ExecuteNonQuery(CommandType.Text, str.ToString())
    End Sub
    Protected Function GetTransationTypes() As DataTable
        Return DBHelper.ExecuteTable(CommandType.Text, "SELECT * FROM TransactionType WHERE Active=1 Order By [TransactionType] Asc")
    End Function

    Protected Function GetPersonNames() As DataTable
        Return DBHelper.ExecuteTable(CommandType.Text, "SELECT * FROM Person Order By [PersonName] Asc")
    End Function

    Protected Sub ClearAllFields()
        txtDate.Text = DateTime.Today.ToString("MMMM d, yyyy")
        txtDesc.Text = ""
        drpType.SelectedIndex = 0
        drpFromTo.SelectedIndex = 0
        txtCash.Text = ""
        txtBank.Text = ""
        txtRecieptDtls.Text = ""
        txtVoucher.Text = GetVoucherNumber()
    End Sub

    Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
        ClearAllFields()
    End Sub
End Class
